package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author FengHuan Wang
 * @Date 2024/12/30 19:00
 * @Version 1.0
 */
@Service
public class SetMealServiceiml implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    /*新增套餐功能实现*/
    @Override
    public void add(SetmealDTO setmealDTO) {
        //将setMealDTO中的属性拷贝到Setmeal中去，分别插入
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);
        Long setmealId = setmeal.getId();
        //然后开始插入套餐内菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历套餐内菜品list集合，为setmealId属性赋值
        setmealDishes.forEach(dishes ->{
            dishes.setSetmealId(setmealId);
        });
        setMealDishMapper.add(setmealDishes);
    }

    /*套餐分页查询功能实现*/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page =  setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /*批量删除套餐功能实现*/
    @Override
    public void deleteByIds(List<Long> ids) {
        setmealMapper.deleteByIds(ids);
    }

    /*查询回显功能实现*/
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal =  setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /*套餐起售停售*/
    @Override
    public void stopAndStart(Integer status, Long id) {
        setmealMapper.stopAndStrat(status,id);
    }

    /*修改套餐功能实现*/
    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        Long id = setmealVO.getId();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        //对于套餐相关的菜品，采用先清空再插入的方式
        setMealDishMapper.deleteBySetmealId(id);
        setMealDishMapper.insertBySetmealId(setmealDishes);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }



}
